import axios from 'axios';

export const TOKEN_OBJ_ID = 'token_json_key';
export const TOKEN_OBJ_JD_PLAY = 'token_json_key_play';


export const KEY_USER_INFO = 'authed_user_menu_info_agent_info';
export const MENU_LIST_ID = 'nav_bar_menu_key';
export const USER_INFO_ID = 'authed_user_info_key';
export const HOST = "http://"+location.hostname+":7777";

export function fetchToken() {
  try {
    //如果扮演的者的token存在就获取扮演者token
    let playToken = sessionStorage.getItem(TOKEN_OBJ_JD_PLAY);
    if (playToken) {
      return JSON.parse(playToken).access_token;
    }

    let tokenString = localStorage.getItem(TOKEN_OBJ_ID);
    if (!tokenString) {
      tokenString = sessionStorage.getItem(TOKEN_OBJ_ID)
    }
    return JSON.parse(tokenString).access_token;
  } catch (e) {
    return '';
  }
}


let isMac = navigator.platform.toUpperCase().indexOf('MAC') >= 0;
let isLocalhost = location.hostname.indexOf('localhost') > -1;
let isEricDev = isMac & isLocalhost;
//初始化一个 axios instance
export function initAxios() {
  let host = HOST;
  let authHeader = 'Bearer ' + fetchToken();
  return axios.create({
    baseURL: host,
    withCredentials: isEricDev,
    timeout: isEricDev ? 9990000 : 10000,
    headers: {'Authorization': authHeader},
  });
}

//清除浏览器储存的token
export function ereaseTokenFromStorage() {
  //发送销毁登陆token的请求
  if (fetchToken()) {
    initAxios().post(`/v1/logout`).then(res => console.log(res));
  }
  //清除token
  localStorage.clear();
  sessionStorage.clear()
}


//##扮演登陆需要在另外一个二级域名下完成

//扮演这个用户,直接注入token
//获取tokenx
export function playRoleTokenInjection(to, from, next) {
  let access_token = to.query.tokenx;
  if (access_token) {
    let obj = {access_token};
    let objString = JSON.stringify(obj);
    sessionStorage.setItem(TOKEN_OBJ_JD_PLAY, objString);

    //清除用户信息
    localStorage.removeItem(KEY_USER_INFO);

    //去掉query string
    next({path: to.path})
  }
}

//很有可能废弃这个函数
//获取用户信息
export function hasTokenAndUserInfoEmptyFetchUserInfo() {
  let userInfo = localStorage.getItem(KEY_USER_INFO);
  let token = fetchToken();
  if (token && !userInfo) {
    initAxios().post(`/v1/userInfo`).then(res => {
      localStorage.setItem(KEY_USER_INFO, JSON.stringify(res.data));
    });
  }
}


export function getStorageUserInfo() {
  let info = JSON.parse(localStorage.getItem(KEY_USER_INFO));
  try {
    return info.user;
  } catch (e) {
    return {
      agent_id: 0,
      role_id: 0,
      phone: '',
      name: '',
      agent: {},
      role: {}
    };
  }
}


export const adminRoles = {
  '系统管理员': 1,
  '系统客服': 2,
  '系统分控': 3,
  '系统财务': 4,
  '系统运营': 5,
};
export const agentRoles = {
  '代理管理员': 11,
  '代理客服': 12,
  '代理财务': 14,
  '代理运营': 15,
  '代理员工': 16,
};
